/**************************************************************/
// 在qt-shutdown.elf工程为模板修改而来，悬浮窗显示电池信息
// #include <qt.hpp>
/**************************************************************/

#include "qt.hpp"
#include <QtGui/QWSServer>

#include "src/batt/batt-main.h"


#ifdef __cplusplus
extern "C" {
#endif


DlgShutdown *g_dialog;
// void *ThreadMinitorKey(void *arg);
// DlgMsg *g_dlgmsg;
int main(int argc, char *argv[])
{	
	// 这句不能放在 QApplication app 之后，否则虽然背景色是白色，但启动
	// 瞬间鼠标背景仍旧绿色
	QWSServer::setBackground(QBrush(QColor(255, 255, 255)));
	
	QApplication app(argc, argv);
	// QApplication::setOverrideCursor(Qt::BlankCursor);

	QFont font;
	font.setPointSize(10); //实际上是16的字号，但设成16却不行
	// font.setFamily(("wenquanyi"));
	font.setBold(false);
	app.setFont(font);

	DlgShutdown dlg;
	dlg.show();
	g_dialog = &dlg;
	// dlg.hide();
	// static pthread_t g_readtimeout;
	// pthread_create(&g_readtimeout, NULL, ThreadMinitorKey, NULL);	
	app.exec();
	exit(4);
}

#if 0

#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h>


/*
 * Linux Kernel 对定时器的应用宏
 */
#define typecheck(type,x) \
	({        type __dummy; \
		typeof(x) __dummy2; \
		(void)(&__dummy == &__dummy2); \
		1; \
	})

#define time_after(a,b) \
	(typecheck(unsigned long, a) && \
	 typecheck(unsigned long, b) && \
	 ((long)(b) - (long)(a) < 0))
#define time_before(a,b) time_after(b,a)

#define time_after_eq(a,b) \
	(typecheck(unsigned long, a) && \
	 typecheck(unsigned long, b) && \
	 ((long)(a) - (long)(b) >= 0))
#define time_before_eq(a,b) time_after_eq(b,a)


/*
 * 不要将MIN_LEVEL调成0，否则及时当背光打开时用户也看不到屏幕内容，
 * 误以为设备损坏，保证最小亮度也是能看到屏幕内容的
*/
#define MIN_LEVEL       (10)				// 可调节最暗亮度
#define AFTER_N_SEC	(5)				// 最后n秒调暗背光
#define LOW_POWER_LEVEL (30)


char *device   = "/dev/input/event0";			// 默认设备
char *mouse_device   = "/dev/input/event2";			// 默认设备
char *bl_dev = "/sys/class/backlight/mxs-bl/";		// LCD背光电源
int timeout   = 30;					// 无操作超时
int level     = 80;					// 关闭前LCD背光亮度
int fd, fd_mouse;


#define _MS (1)

#if 0
#define KEY_RETURN_MAIN 102  // HOME
#else
#define KEY_SHOW_DLG (0x74)  // power
// #define KEY_SHOW_DLG (0x164)  // power2
// #define KEY_SHOW_DLG (0x69)  // right
#endif 

#define HOPE_HOME_S (1000*_MS)
int look_uptime()
{
	int ret, sec;
	struct timeval tv;
    gettimeofday(&tv,NULL);
    sec = tv.tv_sec*1000 + tv.tv_usec/1000;
	return sec;
}



void pseudo_mouse(int fd)
{
	struct input_event event;
	int len;


	event.type = 0x03;
	event.code = 0x18;
	event.value = 1;
	gettimeofday(&event.time, NULL);
	len = write(fd, &event, sizeof(event));


	event.type = 0x03;
	event.code = 1;
	event.value = 0x9000;
	gettimeofday(&event.time, NULL);
	len = write(fd, &event, sizeof(event));
	event.type = 0x03;
	event.code = 0;
	event.value = 0x9001;
	gettimeofday(&event.time, NULL);
	len = write(fd, &event, sizeof(event));

	event.type = EV_SYN;
	event.code = SYN_REPORT;
	event.value = 0;
	gettimeofday(&event.time, NULL);
	len = write(fd, &event, sizeof(event));

	event.type = 0x03;
	event.code = 0x18;
	event.value = 0;
	gettimeofday(&event.time, NULL);
	len = write(fd, &event, sizeof(event));

	event.type = EV_SYN;
	event.code = SYN_REPORT;
	event.value = 0;
	gettimeofday(&event.time, NULL);
	len = write(fd, &event, sizeof(event));
}
void *ThreadMinitorKey(void *arg)
{
	int touch_down = 0;
	struct input_event touch_key;

	fd = open(device, O_RDONLY);
	if (fd < 0) {
		printf("fail open device %s\n", device);
		return 0;
	}
	
	fd_mouse = open(mouse_device, O_RDWR);
	if (fd < 0) {
		printf("fail open device %s\n", mouse_device);
		return 0;
	}

	int len;
	int loop;
	char *keystatus = "unknow";
	unsigned long down_tick;  // 按压时间
	unsigned long press_tick;
	
	while(1) {
		len = read(fd, &touch_key, sizeof(touch_key));
		if (len != -1 && touch_key.type == EV_KEY) {
			touch_down = 1;
			switch(touch_key.value) {
			case 1:
				keystatus = "down";
				if (touch_key.code == KEY_SHOW_DLG) { // key 'home' press
					down_tick = look_uptime();
					// g_dialog->show();
					// system("echo 0 > /sys/class/backlight/mxs-bl/bl_power");
				}
				break;
			case 2:
				if (touch_key.code == KEY_SHOW_DLG) { // key 'home' press
					press_tick = look_uptime();
					// 按压2秒后弹出主窗口
					if (time_after(press_tick, down_tick + HOPE_HOME_S)) {
						pseudo_mouse(fd_mouse);
						g_dialog->show();
						g_dialog->setFocus();
						printf("show window\r\n");
						system("echo 0 > /sys/class/backlight/mxs-bl/bl_power");
						down_tick = down_tick + 1000 * _MS;  // 避免多次进入
					}
				}
				break;
			default:
				keystatus = "unknow";
				break;
			}

		}


	}
	close(fd);
}
#endif

#ifdef __cplusplus
}
#endif




#include "proginfo.h"
MODULE_PROJECT(PRJ_NAME);
#if  defined(PRJ_VERSION) && defined(PRJ_PATCHLEVEL) && defined(PRJ_SUBLEVEL)
	MODULE_VERSION(PRJ_VERSION "." PRJ_PATCHLEVEL "." PRJ_SUBLEVEL);
#endif
MODULE_BUILD(__DATE__ " " __TIME__);
PROG_GITSHA1(__GIT_SHA1__);